package com.github.wxiaoqi.security.biz.biz.activity;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.entity.activity.ActivityCustomer;
import com.github.wxiaoqi.security.biz.entity.activity.ActivityInfo;
import com.github.wxiaoqi.security.biz.entity.commen.Comment;
import com.github.wxiaoqi.security.biz.mapper.ActivityCustomerMapper;
import com.github.wxiaoqi.security.biz.mapper.ActivityInfoMapper;
import com.github.wxiaoqi.security.biz.mapper.CommentMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.cms.request.CmsJoinActivityParam;
import com.github.wxiaoqi.security.common.cms.request.CmsQueryActivityParam;
import com.github.wxiaoqi.security.common.cms.request.CmsSaveActivityParam;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.Query;
import com.github.wxiaoqi.security.common.util.ResponseHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 
 *
 * @author chj
 * @email 
 * @date 2018-07-09 14:10:43
 */
@Slf4j
@Service
public class ActivityInfoBiz extends BaseBiz<ActivityInfoMapper,ActivityInfo> {
	
	@Autowired
	private ActivityCustomerMapper activityCustomerMapper;
	@Autowired
	private CommentMapper commentMapper;
	
	/**
	 * 查询活动列表
	 * @param param
	 * @return
	 */
	public Map<String, Object> queryActivity(CmsQueryActivityParam param) {
		log.info("查询活动列表，请求参数。。。{}"+param);
		 Example example =new Example(ActivityInfo.class);
		 Criteria criteria = example.createCriteria();
		 criteria.andEqualTo("merchantId",param.getMerchantId());
		 criteria.andEqualTo("platformId",param.getPlatformId());
		 example.setOrderByClause("create_time desc");
		 List<ActivityInfo> list = mapper.selectByExample(example);
		 log.info("活动列表。。。{}"+list);
		 
		 Map<String, Object> respMap = new HashMap<>();
		 respMap.put("body", list);
		 respMap.put("code", ResponseCode.OK.getCode());
		 respMap.put("msg", ResponseCode.OK.getMessage());
		 respMap.put("success", true);
		 respMap.put("total", list.size());
		 return respMap;
	}
	
	/**
	 * 查询热门活动列表
	 * @param param
	 * @return
	 */
	public Map<String, Object> queryHotActivity(CmsQueryActivityParam param) {
		log.info("查询热门活动列表，请求参数。。。{}"+param);
		 Example example =new Example(ActivityInfo.class);
		 Criteria criteria = example.createCriteria();
		 criteria.andEqualTo("merchantId",param.getMerchantId());
		 criteria.andEqualTo("platformId",param.getPlatformId());
		 example.setOrderByClause("create_time desc");
		 List<ActivityInfo> list = mapper.selectByExample(example);
		 log.info("热门活动列表。。。{}"+list);
		 
		 Map<String, Object> respMap = new HashMap<>();
		 if (list != null && list.size() > 0) {
			if (list.size() > 3) {
				List<ActivityInfo> list1 = new ArrayList<>();
				for (int i = 0; i < 3; i++) {
					list1.add(list.get(i));
				}
				respMap.put("body", list1);
			}else {
				respMap.put("body", list);
			}
			 respMap.put("code", ResponseCode.OK.getCode());
			 respMap.put("msg", ResponseCode.OK.getMessage());
			 respMap.put("success", true);
		}else {
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
			respMap.put("success", false);
		}
		 return respMap;
	}
	
	
	/**
	 * 查询活动详情
	 * @param param
	 * @return
	 */
	public Map<String, Object> queryActivityInfo(CmsQueryActivityParam param) {
		String id = param.getId();
		String customerId = param.getCustomerId();
		ActivityInfo activityInfo = mapper.selectByPrimaryKey(id);
		Map<String, Object> resMap = new HashMap<>();
		if (activityInfo != null) {
			ActivityCustomer activityCustomer = new ActivityCustomer();
			activityCustomer.setCustomerId(customerId);
			activityCustomer.setActivityId(id);
			ActivityCustomer res = activityCustomerMapper.selectOne(activityCustomer);
			if (res != null) {
				activityInfo.setJoin(res.getJoin());
			}else {
				activityInfo.setJoin("0");
			}
			ActivityCustomer activityCustomer1 = new ActivityCustomer();
			activityCustomer1.setActivityId(id);
			activityCustomer1.setJoin("1");
			Integer count = activityCustomerMapper.selectCount(activityCustomer1);
			activityInfo.setJoinnumber(count);
			List<ActivityCustomer> joins = activityCustomerMapper.select(activityCustomer1);
			activityInfo.setJoinList(joins);
			Comment comment = new Comment();
			comment.setCommentTargetId(id);
			comment.setCommentModuleNo("03");
			Integer count1 = commentMapper.selectCount(comment);
			List<Comment> comments = commentMapper.select(comment);
			activityInfo.setCommentCount(count1);
			activityInfo.setComments(comments);
			
			resMap.put("detail", activityInfo);
			resMap.put("code", ResponseCode.OK.getCode());
			resMap.put("msg", ResponseCode.OK.getMessage());
			resMap.put("success", true);
		}else {
			resMap.put("code", ResponseCode.OK.getCode());
			resMap.put("msg", "活动不存在");
			resMap.put("success", false);
		}
		return resMap;
	}
	
	
	/**
	 * 参加活动
	 * @param param
	 * @return
	 */
	public Map<String, Object> joinActivity(CmsJoinActivityParam param) {
		Map<String, Object> resMap = new HashMap<>();
		String activityId = param.getActivityId();
		ActivityInfo activityInfo = mapper.selectByPrimaryKey(activityId);
		if (activityInfo != null) {
			String customerId = param.getCustomerId();
			Integer contactWay = param.getContactWay();
			String contactValue = param.getContactValue();
			String realName = param.getRealName();
			String id = String.valueOf(SnowFlake.getId());
			ActivityCustomer activityCustomer = new ActivityCustomer();
			activityCustomer.setId(id);
			activityCustomer.setActivityId(activityId);
			activityCustomer.setCustomerId(customerId);
			activityCustomer.setContactWay(contactWay);
			activityCustomer.setRealName(realName);
			activityCustomer.setContactValue(contactValue);
			activityCustomer.setJoin("1");
			activityCustomerMapper.insert(activityCustomer);
			resMap.put("code", ResponseCode.OK.getCode());
			resMap.put("msg", ResponseCode.OK.getMessage());
			resMap.put("success", true);
		}else {
			resMap.put("join", 0);
			return ResponseHelper.success(resMap, null, "01", "活动不存在");
		}
		return resMap;
	}
	
	
	/**
	 * 取消参加活动
	 * @param param
	 * @return
	 */
	public Map<String, Object> cancelJoinActivity(CmsJoinActivityParam param) {
		Map<String, Object> resMap = new HashMap<>();
		String activityId = param.getActivityId();
		ActivityInfo activityInfo = mapper.selectByPrimaryKey(activityId);
		if (activityInfo != null) {
			String customerId = param.getCustomerId();
			ActivityCustomer activityCustomer = new ActivityCustomer();
			activityCustomer.setActivityId(activityId);
			activityCustomer.setJoin("1");
			activityCustomer.setCustomerId(customerId);
			activityCustomerMapper.delete(activityCustomer);
			resMap.put("code", ResponseCode.OK.getCode());
			resMap.put("msg", ResponseCode.OK.getMessage());
			resMap.put("success", true);
		}else {
			resMap.put("success", false);
			return ResponseHelper.success(resMap, null, "01", "活动不存在");
		}
		return resMap;
	}
	
	/**
	 * 添加活动
	 * @param param
	 * @return
	 */
	public Map<String, Object> addActivity(Map<String, Object> param) {
		log.info("添加活动。。。。{}"+param);
		Map<String, Object> respMap = new HashMap<>();
		try {
			String id = String.valueOf(SnowFlake.getId());
			String activityName = (String) param.get("activityName");
			String activityLogo = String.valueOf(param.get("activityLogo"));
			String activityDes = (String) param.get("activityDes");
			String activityContent = (String) param.get("activityContent");
			String activityAddr = (String) param.get("activityAddr");
			String coverImg = String.valueOf(param.get("coverImg"));

			String qrImg = String.valueOf(param.get("qrImg"));
			String publisherName = (String) param.get("publisherName");
			String publisherContactway = (String) param.get("publisherContactway");
			String publisherContactvalue = (String) param.get("publisherContactvalue");
			String acttypeName = (String) param.get("acttypeName");
			String peopleNum = (String) param.get("peopleNum");
			String acttype = (String) param.get("acttype");
			String merchantId= (String) param.get("merchantId");
			String platformId= (String) param.get("platformId");
			String activityStartTime = (String) param.get("activityStartTime");
			String activityEndTime = (String) param.get("activityEndTime");
			String initiationStatus = (String) param.get("initiationStatus");
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			
			ActivityInfo activityInfo = new ActivityInfo();
			activityInfo.setId(id);
			activityInfo.setActivityName(activityName);
			activityInfo.setActivityLogo(activityLogo);
			activityInfo.setActivityDes(activityDes);
			activityInfo.setActivityContent(activityContent);
			activityInfo.setActivityAddr(activityAddr);
			activityInfo.setCreateTime(new Date());
			activityInfo.setMerchantId(merchantId);
			activityInfo.setPlatformId(platformId);
			activityInfo.setActivityStartTime(format.parse(activityStartTime));
			activityInfo.setActivityEndTime(format.parse(activityEndTime));
			activityInfo.setCoverImg(coverImg);
			activityInfo.setQrImg(qrImg);
			activityInfo.setPublisherName(publisherName);
			activityInfo.setPublisherContactway(Integer.valueOf(publisherContactway));
			activityInfo.setPublisherContactvalue(publisherContactvalue);
			activityInfo.setFavortCount(0);
			activityInfo.setCommentCount(0);
			activityInfo.setActtypeName(acttypeName);
			activityInfo.setActtype(Integer.valueOf(acttype));
			activityInfo.setPeopleNum(Integer.valueOf(peopleNum));
			activityInfo.setInitiationStatus(Integer.valueOf(initiationStatus));
			mapper.insert(activityInfo);
			respMap.put("code", "00");
			respMap.put("msg", "成功");
		} catch (Exception e) {
			respMap.put("code", "01");
			respMap.put("msg", "异常");
		}
		return respMap;
	}
	
	
	/**
	 * 分页查询活动
	 * @param param
	 * @return
	 */
	public Map<String, Object> queryActivitiesWithPage(Map<String, Object> param) {
		log.info("查询活动列表。。。。{}"+param);
		Map<String, Object> respMap = new HashMap<>();
		try {
			Query query = new Query(param);
			Page<ActivityInfo> result = PageHelper.startPage(query.getPage(), query.getLimit());
			List<ActivityInfo> list = mapper.queryActivities(param);
			TableResultResponse<ActivityInfo> table = new TableResultResponse<>(result.getTotal(), list);
			respMap.put("code", "00");
			respMap.put("msg", "查询成功");
			respMap.put("body", table);
		} catch (Exception e) {
			respMap.put("code", "01");
			respMap.put("msg", "查询异常");
		}
		return respMap;
	}
	
	
	/**
	 * 关闭活动
	 * @param param
	 * @return
	 */
	public Map<String, Object> closeActivity(Map<String, Object> param) {
		log.info("关闭活动。。。{}"+param);
		Map<String, Object> respMap = new HashMap<>();
		ActivityInfo act = mapper.selectByPrimaryKey((String) param.get("id"));
		if (act != null) {
			act.setInitiationStatus(2);
			mapper.updateByPrimaryKey(act);
			respMap.put("code", "00");
			respMap.put("msg", "修改成功");
		}else {
			respMap.put("code", "01");
			respMap.put("msg", "活动不存在");
		}
		return respMap;
	}
	
	
	
	
	
}